package com.yanqu.road.server.cmd.activity.hsisland.cross;

import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.activity.args.ActivityValueParamArgs;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.bussiness.player.UserActivityBussiness;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.HsIslandProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.fund.ActivityFundMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.List;

@Cmd(code = Protocol.S_HS_ISLAND_END_UNION_SCORE_CHANGE_FROM_CROSS, desc = "海神岛-跨服返回-活动结束更新玩家商会成就分，基金分")
public class HsIslandUpdateUnionScoreEndActivityFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        HsIslandProto.HsIslandUnionScoreChangeEndActivityFromCrossMsg reqMsg = HsIslandProto.HsIslandUnionScoreChangeEndActivityFromCrossMsg.parseFrom(packet.getBytes());
        //商会成就condition变更
        if (player != null) {
            //玩家在线
            ActivityValueParamArgs args = new ActivityValueParamArgs(reqMsg.getActivityId(), reqMsg.getMaxFloorId(), reqMsg.getMaxFloodProgress());
            player.notifyListener(eGamePlayerEventType.HsIslandUnionAchievement.getValue(), args);
        } else {
            synchronized (GamePlayerMgr.getPlayerLock(packet.getPlayerId())) {
                //玩家不在线，更新数据库
                long now = System.currentTimeMillis() / 1000;
                List<UserActivityConditionData> tmpConditionDataList = UserActivityBussiness.getUserActivityConditionDataListByUserIdAndType(reqMsg.getActivityId(), packet.getPlayerId(), eGamePlayerEventType.HsIslandUnionAchievement.getValue());
                for (UserActivityConditionData tmpConditionData : tmpConditionDataList) {
                    if (!tmpConditionData.isGetReward()) {
                        if (BigInteger.valueOf(reqMsg.getMaxFloorId()).compareTo(tmpConditionData.getValue()) > 0
                                || (BigInteger.valueOf(reqMsg.getMaxFloorId()).compareTo(tmpConditionData.getValue()) == 0 && Integer.parseInt(reqMsg.getMaxFloodProgress()) > Integer.parseInt(tmpConditionData.getParam()))) {
                            tmpConditionData.setValue(BigInteger.valueOf(reqMsg.getMaxFloorId()));
                            tmpConditionData.setParam(reqMsg.getMaxFloodProgress());
                            tmpConditionData.setModifyTime(now);
                            // 更新condition
                            UserActivityBussiness.updateUserActivityConditionData(tmpConditionData);
                        }
                    }
                }
            }
        }
        //基金分变更
        ActivityFundMgr.updateProgress(reqMsg.getActivityId(), packet.getPlayerId(), reqMsg.getUnionScore());
    }

    @Override
    public void sendData(GamePlayer player) {}
}
